/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：FrontUserDaoImpl.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IFrontUserDao;
import com.xteam.ecsystem.entities.BaseCartInfo;
import com.xteam.ecsystem.entities.BaseInfoItem;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.BaseMemberInfo;
import com.xteam.ecsystem.entities.BasePasswordQuestion;
import com.xteam.ecsystem.utils.FrontMemberItemsCS;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 前端用户数据访问实现类
 * @author liuzhi
 *
 */
@Repository
public class FrontUserDaoImpl extends DDLPrefix implements IFrontUserDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	@SuppressWarnings("unchecked")
	public List<BasePasswordQuestion> getQuestionList() {
		List<BasePasswordQuestion> list = new ArrayList<BasePasswordQuestion>();
		
		try {
			
			String hql = SQL_FROM + BasePasswordQuestion.REF + SQL_WHERE + " bpqState=1";
			list = baseDao.find(hql);
			
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<BaseInfoItem> getMemberItemList() {
		List<BaseInfoItem> list = new ArrayList<BaseInfoItem>();
		
		try {
			
			String hql = SQL_FROM + BaseInfoItem.REF + SQL_WHERE + "biiIsReg=1" 
				+ SQL_AND + " biiState=1" + SQL_ORDERBY + "biiOrder";
			list = baseDao.find(hql);
			
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List getAllMemberItems(String bmiMember) {
		List listObj = new ArrayList();
		List list = new ArrayList();
		FrontMemberItemsCS fmi = null;
		
		try {
			String sql = SQL_SELECT 
					+ "bii.bii_id, bii.bii_name, bii.bii_type, bii.bii_content, bii.bii_order, " 
					+ "bii.bii_state, bii.bii_is_reg, bmi.bmi_id, bmi.bmi_member, bmi.bmi_item, " 
					+ "bmi.bmi_value "
					+ SQL_FROM
					+ "base_info_item AS bii "
					+ "left join "
					+ "base_member_info AS bmi "
					+ SQL_ON
					+ "bii.bii_id = bmi.bmi_item "
					+ SQL_AND
					+ "bmi.bmi_member = '" + bmiMember + "'";
			
			list = this.baseDao.queryListWithSql(sql);
			
			for (int i = 0; i < list.size(); i++) {
				String[] d = StringUtil.objToString((Object[])list.get(i)); 
				
				int d2 = Integer.parseInt(d[2]);
				int d4 = Integer.parseInt(d[4]);
				int d5 = Integer.parseInt(d[5]);
				int d6 = Integer.parseInt(d[6]);
				
				//构造实体以便freemarker处理
				fmi = new FrontMemberItemsCS(d[0], d[1], d2, d[3], d4, d5, d6, d[7], d[8], d[9], d[10]);
				listObj.add(fmi);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return listObj;
		
	}

	public String saveOrUpdateBMB(Object obj, String id) {
		try {
			BaseMemberBase bmbObj = (BaseMemberBase)obj;
			
			if (StringUtil.isNotNullOrEmpty(id)){
				this.baseDao.update(bmbObj);
				return id;
			}else {
				this.baseDao.save(obj);
				return bmbObj.getBmbId();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void saveOrUpdateBMI(Object obj, String id) {
		try {
			if (StringUtil.isNotNullOrEmpty(id)){
				this.baseDao.update(obj);
			}else {
				this.baseDao.save(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void saveMuchObj(List listObj) {
		try {
			if (listObj != null && !listObj.isEmpty()) {
				this.baseDao.batchSave(listObj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBByEmail(String bmbEmail) {
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbEmail = ?";
			list = baseDao.find(hql, new Object[]{bmbEmail});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBByNick(String bmbNick) {
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbNick = ?";
			list = baseDao.find(hql, new Object[]{bmbNick});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBByActCode(String bmbActivator) {
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbActivator = ?";
			list = baseDao.find(hql, new Object[]{bmbActivator});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBByNickAEmail(String bmbNick, String bmbEmail) {
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbNick = ? and bmbEmail = ?";
			list = baseDao.find(hql, new Object[]{bmbNick, bmbEmail});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBById(String bmbId) {
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbId = ?";
			list = baseDao.find(hql, new Object[]{bmbId});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBByEmailAPwd(String emailOrUserName, String bmbPassword){
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbPassword = ?";
			
			if(StringUtil.isNotNullOrEmpty(emailOrUserName) && emailOrUserName.lastIndexOf("@") > 0){
				hql += SQL_AND + "bmbEmail=?";
			}else{
				hql += SQL_AND + "bmbNick=?";
			}
			
			list = baseDao.find(hql, new Object[]{bmbPassword, emailOrUserName});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BasePasswordQuestion getBpqById(String bpqId){
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BasePasswordQuestion.REF + SQL_WHERE + "bpqId = ?";
			list = baseDao.find(hql, new Object[]{bpqId});
			
			if (list != null && !list.isEmpty()) {
				return (BasePasswordQuestion)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseMemberBase getBMBByBmbAnswer(String bmbId, String bmbAnswer){
		List list = new ArrayList();
		
		try {
			String hql = SQL_FROM + BaseMemberBase.REF + SQL_WHERE + "bmbId = ? and bmbAnswer = ?";
			list = baseDao.find(hql, new Object[]{bmbId, bmbAnswer});
			
			if (list != null && !list.isEmpty()) {
				return (BaseMemberBase)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	public void delBmiByMemberId(String bmiMember){
		String hql = "";
		
		try {
			hql = SQL_DELETE + SQL_FROM + BaseMemberInfo.REF + SQL_WHERE + "bmiMember = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{bmiMember});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateUserStatus(String loginUserId, String cartUserId){
		String hql = "";
		
		try {
			hql = SQL_UPDATE + SQL_FROM + BaseCartInfo.REF + " set bciUser = ?" 
				+ SQL_WHERE + "bciUser = ?";
			
			this.baseDao.executeUpdate(hql, new Object[]{loginUserId, cartUserId});
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
